package main.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Scanner;
import org.jagatoo.logging.FileLog;
import org.jagatoo.logging.Log;
import org.jagatoo.logging.LogChannel;
import org.jagatoo.logging.LogLevel;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import main.java.data.Configuration;

/**Manages Textures, Loading data etc...
 * 
 * @author Sven Berger
 *
 */
public class ResourceManager
{
	
	/* Configuration load method
	 * 
	 * Restrictions: 
	 * No Integrity check 
	 * non handled Exceptions
	 * 
	 */
	private LogChannel logChannel=null;
	private FileLog filelog=null;
	
	/**
	 * @return  Configuration
	 * @throws Exception
	 */
	@SuppressWarnings("nls")
	public Configuration loadConfig() throws Exception
	{
		//look for existent configuration data
		
		File file=new File("config.xml");
		Configuration config=null;
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("Configuration", Configuration.class);
		
		if(file.isFile())
		{
			Scanner reader=new Scanner(new FileInputStream(file));
			String xml="";
			while ( reader.hasNextLine() )
			{
		        xml+=(reader.nextLine());
		    }

			//System.out.println(xml);
			try
			{
				config= (Configuration)xstream.fromXML(xml.toString());
			}
			//if exception on loading config reset to default configuration
			catch(Exception e)
			{
				config= new Configuration();
			}
		
		}
		//create configuration file
		else
		{
			file.createNewFile();
			config= new Configuration();
		}
		//if there is a new option to the configuration data drop it to the configuration file
		FileWriter writer=new FileWriter(file,false);
		writer.write(xstream.toXML(config));
		writer.close();
		return(config);
	}
	/**
	 * @param log
	 */
	public void register_Log(final LogChannel log)
	{
		this.logChannel=log;
	}
	/**
	 * @param logfile
	 */
	public void setLogfile(final FileLog logfile)
	{
		this.filelog=logfile;
	}
	/**
	 * @param msg
	 */
	public void addMessag(final String msg)
	{
		Log.println(this.logChannel, LogLevel.REGULAR,msg);
	}
	/**
	 * @param msg
	 */
	public void addDebug(final String msg)
	{
		Log.println(this.logChannel, LogLevel.DEBUG,msg);
	}
	/**
	 * @param msg
	 */
	public void addError(final String msg)
	{
		Log.println(this.logChannel, LogLevel.ERROR,msg);
	}
	/**
	 * if true only internal msg are displayed, on false all msg are displayed (engine debugging output)
	 * @param activate
	 */
	public void filter(final boolean activate)
	{
		if(activate)
		{
			this.filelog.setChannelFilter(this.logChannel.getID());
			return;
		}
		this.filelog.setChannelFilter(0);
	}
	/**
	 * @param lvl
	 */
	public void setFilterLevel(final int lvl)
	{
		this.filelog.setLogLevel(lvl);
	}
	/**
	 * @return LogChannel
	 */
	public LogChannel getLogchannel()
	{
		return this.logChannel;
	}

}
